package bigdata;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.TextArea;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.BorderFactory;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JList;
import javax.swing.JTextPane;
import javax.swing.JTextArea;
import java.awt.ScrollPane;

public class SimulationWindow extends JFrame {

	private JPanel contentPane;
	private JTextArea textArea;

	/**
	 * Create the frame.
	 */
	public SimulationWindow() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		textArea = new JTextArea();
		contentPane.add(textArea, BorderLayout.CENTER);

		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		textArea.setFont(new Font("Arial", Font.PLAIN, 15));
		textArea.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
		textArea.setEditable(false);

		JScrollPane scroll = new JScrollPane(textArea);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

		contentPane.add(scroll);
	}

	public void addUserToList(VisitData visitor) {

		int status = visitor.status;
		String userStory;

		switch (status) {

		case 1:

			userStory = new String("User: " + visitor.visitId.toString()
					+ " From: " + visitor.country.toString()
					+ " entered page: " + visitor.landingPage.toString() + "\n");

			if (isSpecialCountry(visitor.country))

				userStory += "user gets special country bonus!!\n";

			if (isSpecialWebSite(visitor.referrer))

				userStory += "user came from a special webSite!!\n";

			textArea.setText(textArea.getText() + userStory + "\n\n");

			break;
		case 2:

			userStory = new String("User: " + visitor.visitId.toString()
					+ " From: " + visitor.country.toString()
					+ " Searched for: " + visitor.searchQuery + "\n");

			if (isSpecialCountry(visitor.country))

				userStory += "user gets special country bonus!!\n";

			if (isSpecialWebSite(visitor.referrer))

				userStory += "user came from a special webSite!!\n";

			textArea.setText(textArea.getText() + userStory + "\n\n");

			break;
		case 3:

			userStory = new String("User: " + visitor.visitId.toString()
					+ " From: " + visitor.country.toString() + " came from: "
					+ visitor.referrer.toString() + "\n");

			if (isSpecialCountry(visitor.country))

				userStory += "user gets special country bonus!!\n";

			if (isSpecialWebSite(visitor.referrer))

				userStory += "user came from a special webSite!!\n";

			textArea.setText(textArea.getText() + userStory + "\n\n");

			break;

		case 4:
			userStory = new String("User: " + visitor.visitId.toString()
					+ " From: " + visitor.country.toString() + " came from: "
					+ visitor.referrer.toString() + "\n");

			if (isSpecialCountry(visitor.country))

				userStory += "user gets special country bonus!!\n";

			if (isSpecialWebSite(visitor.referrer))

				userStory += "user came from a special webSite!!\n";

			textArea.setText(textArea.getText() + userStory + "\n\n");

			break;
		}
	}

	private boolean isSpecialCountry(String country) {
		if (country.equals("THAILAND"))

			return true;

		else if (country.equals("GERMANY"))

			return true;

		else
			return false;

	}

	private boolean isSpecialWebSite(String website) {

		if (website.equals("www.google.com"))

			return true;

		else if (website.equals("www.ebay.com"))

			return true;

		else
			return false;
	}
}
